package com.szewec.ps.ui.task;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.mapapi.map.MyLocationData;
import com.szewec.ps.R;
import com.szewec.ps.base.BaseActivity;
import com.szewec.ps.base.Constants;
import com.szewec.ps.databinding.ActivitySurveyObjEditBinding;
import com.szewec.ps.model.MyPoint;
import com.szewec.ps.model.PresentValue;
import com.szewec.ps.model.ViewModeValue;
import com.szewec.ps.model.task.SurveyObjectModel;
import com.szewec.ps.myview.toast.bamtoast.BamToast;
import com.szewec.ps.ui.common.SelectCoordinatesActivity;
import com.szewec.ps.util.CommonUtils;
import com.szewec.ps.util.ListUtil;
import com.szewec.ps.util.LogUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.dialog.LoadingDialog;

import java.math.BigDecimal;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class SurveyObjectEditActivity extends BaseActivity {
    private ActivitySurveyObjEditBinding _binding;
    private LoadingDialog _loadingDialog;

    // 坐标
    private List<MyPoint> pointList;

    private String ModeFlag_Add_Edit_View; //标志当前界面是详情模式，还是新增模式，还是编辑模式!
    private SurveyObjectModel surveyObject = new SurveyObjectModel();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        _binding = DataBindingUtil.setContentView(this, R.layout.activity_survey_obj_edit);
        ButterKnife.bind(this);
        _loadingDialog = LoadingDialog.getInstance(this);

        initData();

        initTitleBar();
    }

    private void initData(){
        PresentValue<SurveyObjectModel> presentValue = (PresentValue<SurveyObjectModel>) getIntent().getSerializableExtra(Constants.EXTRA_KEY.SURVEY_OBJECT);
        //获取是新增还是修改还是详情展示
        ModeFlag_Add_Edit_View = presentValue.getFlag();
        surveyObject = presentValue.getT();

        initView();
    }

    private void initTitleBar(){
        ViewModeValue viewModeValue = new ViewModeValue(this, ModeFlag_Add_Edit_View, _binding.titleBar, _binding.llContainer);

        //设置导航栏右侧按钮
        if (Constants.PRESENT_VALUE_FLAG.VIEW.equals(ModeFlag_Add_Edit_View)){//详情预览
            CommonUtils.setViewMode(viewModeValue, this::finish, this::callMoreMenu);
            _binding.titleBar.setTitle("调查对象详情");
        }
        else if (Constants.PRESENT_VALUE_FLAG.ADD.equals(ModeFlag_Add_Edit_View)){
            CommonUtils.setViewMode(viewModeValue, null, this::appendOrModifyData);
            _binding.titleBar.setTitle("调查对象新建");
            showSaveBtn();
        }
        else if (Constants.PRESENT_VALUE_FLAG.EDIT.equals(ModeFlag_Add_Edit_View)){
            CommonUtils.setViewMode(viewModeValue, this::returnCheck, this::appendOrModifyData);
            _binding.titleBar.setTitle("调查对象编辑");
            showSaveBtn();
        }
        //显示返回按钮
        _binding.titleBar.setLeftLayoutVisibility(View.VISIBLE);
        _binding.titleBar.setLeftLayoutClickListener(this::finish);
    }

    /**
     * 显示保存按钮
     */
    private void showSaveBtn() {
        //设置右边按钮
        _binding.titleBar.setRightTextVisibility(View.VISIBLE);
        _binding.titleBar.setRightText("保存");
        _binding.titleBar.setRightLayoutClickListener(() -> {
            String surveyName = _binding.surveyName.getStringValue();
            if (StringUtils.isEmpty(surveyName)) {
                BamToast.showText(this, R.string.requires_text_filed_check, false);
            } else if (ListUtil.isEmpty(pointList) || pointList.size() < 1) {
                BamToast.showText(this, "请添加1个以上的点！", false);
            } else {
                surveyObject.setSurveyName(surveyName);
                surveyObject.setCoordinate(JSONArray.toJSONString(pointList));
                // 如果ID不为空，说明是修改
                String flag = StringUtils.isNotEmpty(surveyObject.getTaskId()) ? Constants.PRESENT_VALUE_FLAG.EDIT : Constants.PRESENT_VALUE_FLAG.ADD;
                Intent intent = new Intent();
                PresentValue<SurveyObjectModel> presentValue = new PresentValue<>(flag,surveyObject);
                intent.putExtra(Constants.EXTRA_KEY.SURVEY_OBJECT, presentValue);
                setResult(Constants.RESULT_CODE.NEED_REFRESH, intent);
                finish();
            }
        });
    }

    /**
     * 填充数据
     */
    private void initView() {
        _binding.surveyName.setValue(surveyObject.getSurveyName());
        // 填充坐标
        if (StringUtils.isNotEmpty(surveyObject.getCoordinate())) {
            pointList = JSONArray.parseArray(surveyObject.getCoordinate(), MyPoint.class);
            _binding.locationEdit.setText("查看路段");
        }
//        _binding.locationEdit.setValue(surveyObject.getCoordinate());
        // 如果是查看界面 将名称和坐标设置成只读
        if (Constants.PRESENT_VALUE_FLAG.VIEW.equals(ModeFlag_Add_Edit_View)) {
            _binding.surveyName.switchoverMode(false);
//            _binding.locationEdit.switchoverMode(false);
        }
    }


    private void appendOrModifyData() {
    }

    private void deleteData(){
    }

    private void returnCheck(){
        finish();
    }

    private void callMoreMenu(){
        CommonUtils.showMoreMenu(this, _binding.titleBar.getRightLayout(), this::linkToEditActivity, this::deleteData);
    }

    private void linkToEditActivity() {
    }

    @OnClick({R.id.locationEdit})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.locationEdit) {
            String point = null;
            Intent intent = new Intent(this, SelectCoordinatesActivity.class);
            if (ListUtil.isNotEmpty(pointList)) {
                point = JSONArray.toJSONString(pointList);
                _binding.locationEdit.setText("查看路段");
            }
            PresentValue<String> presentValue = new PresentValue<>(Constants.SELECT_COOR_FLAG.SELECT_COOR, point);
            intent.putExtra(Constants.EXTRA_KEY.SELECT_COOR, presentValue);
            registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), v -> {
                if (v.getData() != null) {
                    pointList = JSONObject.parseArray((v.getData().getStringExtra(Constants.EXTRA_KEY.SELECT_COOR)), MyPoint.class);
                }
            }).launch(intent);
        }
    }

}
